#include "MephistoCAM.h"

int binstr2int(char *str, int *x){

	int str_len = strlen(str);
	char tmp[128];

	strncpy(tmp,str,2);
	if( strcmp(tmp, "0b") == 0 || strcmp(tmp, "0B") == 0 ){
		memset(tmp,0,128);
		strcpy(tmp,str+2);
	} else {
		printf("Error: *str = %s\n", str);
		return _FAILURE_;
	}

	*x = 0;
	int i_max = str_len-2;
	for( int i=0; i<i_max; i++ ){
		if( tmp[i] == '1' ){
			*x += 1 << (i_max-i-1);
		}
	}

	return _SUCCESS_;
}

// 判断字符串是否是表示曝光时间长度的
int isExpTimeTag( char *str ){
    char *ptr = strchr(str, 'd');
    
    int res = -1;
    if( NULL != ptr )
        res = 1;
    else
        res = 0;
        
    return res;
}

// 从表示曝光时间长度的字符串中提取曝光时间,字符串采用类似"12345d567"这样的格式
double extractExpTime( char *str ){

    int str_len = strlen(str);
    char *ptr_d = strchr(str, 'd');
    int idx_d = ptr_d - str;
    
    int i = 0;
    int exp_t_int = 0;      // integer part
    double exp_t_dec = 0;   // decimal part
    
    while( i < str_len ){
        // printf(" I = %3d\n", i);
        
        if( i < idx_d ){
            exp_t_int += (str[i]-'0')*pow(10, idx_d - i -1);
        }
        
        if( i > idx_d ){
            exp_t_dec += (str[i]-'0')*pow(10, idx_d - i);
        }
        
        i++;
    }
    double exp_t = exp_t_int + exp_t_dec;

    return exp_t;
}


double time_diff(struct timeval *start, struct timeval *end){
    return (end->tv_sec - start->tv_sec) + 1e-6*(end->tv_usec - start->tv_usec);
}

